/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package occho.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import occho.model.Log;
import occho.model.Pregunta;
import occho.model.Usuario;

/**
 *
 * @author Migue 
 */
public class OracleLogDAO implements LogDAO{
 private Connection conn = null;
     
 /**
  * Constructor de la clase
  * @param conn
  * @throws SQLException
  * @throws ClassNotFoundException
  */
 public OracleLogDAO(Connection conn) throws SQLException, ClassNotFoundException {
        this.conn =conn;
    }
 
 @Override
 public List<Log> SeleccionarLogs() {
        Log l = new Log();
        Usuario u = new Usuario();
        ArrayList<Log> logs = new ArrayList<Log>();
        try {
            OracleDAOFactory daoFactory = OracleDAOFactory.getInstancia();
            LogDAO logDAO = daoFactory.getLogDAO();
            UsuarioDAO uDAO = daoFactory.getUsuarioDAO();
            PreparedStatement stmt = conn.prepareStatement("select * from log order by fecha desc");
            stmt.clearParameters();
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
               l = new Log();
               l.setUsuario(uDAO.seleccionarUsuario(rs.getString("email")));
               l.setFecha(rs.getTimestamp("fecha"));
               l.setAccion(rs.getString("accion"));
               logs.add(l);
            }
            rs.close();
            stmt.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(OracleLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(OracleLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return logs;
    }
    
 @Override
 public boolean insertarLog(Log l) throws SQLException, ClassNotFoundException {
        PreparedStatement stmt = conn.prepareStatement("insert into log (idusuario,fecha,accion,email) values(?,?,?,?)");
        stmt.clearParameters();
        stmt.setInt(1, l.getUsuario().getIdUsuario()); 
        stmt.setTimestamp(2,new Timestamp(Calendar.getInstance().getTimeInMillis())); 
        stmt.setString(3, l.getAccion());
        stmt.setString(4, l.getUsuario().getEmail());
        
        
        
        if (stmt.executeUpdate() > 0) {
            stmt.close();
            return true;
        } else {
            stmt.close();
            return false;
        }
    }

 @Override
 public boolean resetLog() throws SQLException, ClassNotFoundException {
        PreparedStatement stmt = conn.prepareStatement("delete from log");
        stmt.clearParameters();
        
        if (stmt.executeUpdate() > 0) {
            stmt.close();
            return true;
        } else {
            stmt.close();
            return false;
        }
        
        
    }
}
